#include "cmdtreasure.h"
#include "../mainctrl.h"
#include "../cmd/dataprepare.h"
#include "commmsg.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_TREASURE, CCmdTreasure);

int CCmdTreasure::Do(void* pData)
{
    CT_ASSERT(pData);

    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_TREASURE ||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_TREASURE,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);

    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam); //消息解析
    if (iRet)
    {
        return AnsClients(iRet);
    }

	chSubCmd = stReq.sub_type();
    uiGID = stReq.gid();

    //检查登录
    iRet = pstMainCtrl->CheckLogin(uiGID, stEnpHead);
    if (iRet != RET_OK)
    {
        return AnsClients(iRet);
    }

    stAns.set_gid((int)uiGID);
    stAns.set_sub_type(chSubCmd);

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdTreasure::LogicDo(int iRet)
{
    if (iRet != 0)
    {
        return AnsClients(iRet);
    }

    int iIndex = 0;

    PREPARE_PLAYER_BASE_INFO(this, uiGID);
    ASSERT_PLAYER_PREPARED(uiGID);

    PREPARE_PLAYER_DETAIL_INFO(this, uiGID);
    ASSERT_PLAYER_DETAIL_PREPARED(uiGID);

    PREPARE_PLAYER_ONLINE_DATA(this, uiGID);

    iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGID);
    if (iIndex < 0)
	{
        CT_ERROR(("no base data user(%u)", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
	}
	CPlayerBaseInfo&
        stPlayerBase = pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];

    if (stPlayerBase._ucIsInited != 1)
    {
        return AnsClients(ERROR_NOT_INITED);
    }

    iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(uiGID);
    if (iIndex < 0)
	{
        CT_ERROR(("no detail data user(%u)", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
	}
	CPlayerDetailInfo&
        stPlayerDetail = pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

    iIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("user %u not in m_stPlayerOnlineDataHash", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);

    }
    CPlayerOnlineData& stPlayerOnlineData =
            pstMainCtrl->m_stPlayerOnlineDataHash[iIndex];

    //检查系统是否开启
    if (!pstMainCtrl->m_stCommConfigLoader.IsOpen(SYS_OPEN_COND_ID_20, stPlayerBase, stPlayerOnlineData))
    {
        return AnsClients(ERROR_SYS_NOT_OPEN);
    }

	switch (chSubCmd)
	{
	case CTreasureMgr::CMD_TREASURE_SUB_UP_LEVEL:
        {
    	    iRet = pstMainCtrl->m_stTreasureMgr.TreasureUpLevel(stPlayerBase,
                stPlayerDetail, stPlayerOnlineData, stReq);
        }
		break;
	case CTreasureMgr::CMD_TREASURE_SUB_PUT_ON:
        {
            iRet = pstMainCtrl->m_stTreasureMgr.TreasurePutOn(stPlayerBase,
                stPlayerDetail, stPlayerOnlineData, stReq);
            if (RET_OK == iRet)
            {
                //触发任务
                pstMainCtrl->m_stTaskMgr.TriggerDoSomeOpt(
                    stPlayerOnlineData._stUserTask, TASK_OPT_ID_107, 1);
            }
        }
		break;
	case CTreasureMgr::CMD_TREASURE_SUB_TAKE_OFF:
        {
    	    iRet = pstMainCtrl->m_stTreasureMgr.TreasureTakeOff(stPlayerBase,
                stPlayerDetail, stPlayerOnlineData, stReq);
        }
		break;
	default:
		iRet = ERROR_TREASURE_SUB_CMD;
		break;
	}

    if (iRet)
    {
        CT_TRACE(("user(gid=%u) deal treasure op(sub=%d) error(%d)",uiGID,
            chSubCmd,iRet));
        return AnsClients(iRet);
    }

	if (chSubCmd == CTreasureMgr::CMD_TREASURE_SUB_UP_LEVEL)
	{
    	//保存数据 base info
    	CVarObjectKey stKey;
    	stKey.m_chDataType = DT_PLAYER_BASE_INFO;
    	stKey.m_u64ID = uiGID;
    	pstMainCtrl->UpdateData(stKey, stPlayerBase, VOP_UPDATE);
	}

	if (chSubCmd == CTreasureMgr::CMD_TREASURE_SUB_UP_LEVEL ||
		chSubCmd == CTreasureMgr::CMD_TREASURE_SUB_PUT_ON ||
		chSubCmd == CTreasureMgr::CMD_TREASURE_SUB_TAKE_OFF)
	{
    	//保存数据 detail info
    	CVarObjectKey stKey;
    	stKey.m_chDataType = DT_PLAYER_DETAIL_INFO;
    	stKey.m_u64ID = uiGID;
    	pstMainCtrl->UpdateData(stKey, stPlayerDetail, VOP_UPDATE);
	}

    return AnsClients(RET_OK);
}

int CCmdTreasure::AnsClients(int iRet)
{
    pstMainCtrl->m_stEncodeBuf.Clear();

    stMsgHead.set_msgret(iRet);
    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);

    if (iRet != RET_OK)
    {
        pstMainCtrl->ReturnMsgHead(stMsgHead, stEnpHead);
        return Done(iRet);
    }

    pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
    pstMainCtrl->EncodePBMsg(stAns, pstMainCtrl->m_stEncodeBuf);

    if(pstMainCtrl->AnswerToClient(stEnpHead,pstMainCtrl->m_stEncodeBuf,true))
    {
        return Done(RET_SYS_ERROR);
    }

    return Done(iRet);
}

